package net.avh4.scratch.rpg.battle;

import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.imageio.ImageIO;

import net.avh4.scratch.rpg.model.GameModel;
import net.avh4.scratch.rpg.model.Stat;

public class Character {
	
	private Image battleImage;
	
	private HashMap<String, Stat> stats = new HashMap<String, Stat>();
	
	public Character(File imageFile) throws IOException {
		battleImage = ImageIO.read(imageFile);
	}
	
	public Character(String imageFilename) throws IOException {
		this(new File(imageFilename));
	}
	
	/**
	 * 
	 * @param key
	 * @return The mutable stat object.
	 */
	public Stat getStat(String key) {
		Stat stat = stats.get(key);
		if (stat == null) {
			stat = GameModel.getDefaultStat("HP");
			stats.put(key, stat);
		}
		return stat;
	}
	
	public void draw(Graphics2D g, int x, int y) {
		
		g.drawImage(battleImage, x, y, null);
	}

	public Action getNextAction(Battle battle) {
		if (getStat("HP").getPercent() < 0.25) {
			return new RecoveryAction(this);
		} else {
			return new MeleeAction(this, battle.getOpponents(this).get(0));
		}
	}

}
